Add configurable cache modes and fallback policies for non-isbits types#34
Merged
ChrisRackauckas merged 2 commits intoMar 30, 2026
Merged
Conversation
Replaces the boolean `FB` type parameter with two orthogonal configuration axes:
Cache modes (how fallback FunctionWrappers are cached):
- NoCache: dynamic dispatch every call (1 alloc, legacy behavior)
- SingleCache: cache one FunctionWrapper for last arg types (0 alloc on hit)
- DictCache: cache per arg type in a Dict (0 alloc, multi-type)
Fallback policies (when fallback is allowed):
- Strict: always error on mismatch (legacy Val{false})
- AllowAll: always fall back (legacy Val{true})
- AllowNonIsBits: fall back only for non-isbits types, error for isbits mismatches
Default is SingleCache + AllowNonIsBits, which provides zero-allocation
fallback for BigFloat, SparseConnectivityTracer, and similar non-isbits types
while still catching isbits type mismatches as bugs.
The key mechanism is lazily creating and caching a FunctionWrapper for the
actual argument types on first fallback hit. Subsequent calls use the cached
wrapper's ccall path with no extra allocations.
Legacy API (Val{true}/Val{false} and FunctionWrappersWrapper{FW,Bool})
is preserved for backward compatibility, mapping to AllowAll/Strict + NoCache.
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Breaking change: removes the boolean FB type parameter and Val{true}/Val{false}
API. The FunctionWrappersWrapper type now takes a fallback policy and cache
storage type as type parameters instead.
Cache modes (how fallback FunctionWrappers are cached):
- NoCache: dynamic dispatch every call (1 alloc per call)
- SingleCache: lazily cache a FunctionWrapper for last-seen arg types (0 alloc)
- DictCache: cache per arg type in a Dict (0 alloc, multi-type)
Fallback policies (when fallback is allowed):
- Strict: always error on type mismatch
- AllowAll: always fall back to original function
- AllowNonIsBits: fall back for non-isbits types, error for isbits mismatches
Default: SingleCache + AllowNonIsBits
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
ChrisRackauckas-Claude
pushed a commit
to ChrisRackauckas-Claude/SciMLBase.jl
that referenced
this pull request
Mar 30, 2026
FunctionWrappersWrappers v1.0.0 (SciML/FunctionWrappersWrappers.jl#34) adds configurable cache modes and fallback policies for non-isbits types. This is non-breaking — it strictly allows more types through the wrapper. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com> Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
NoCache,SingleCache,DictCache) and fallback policies (Strict,AllowAll,AllowNonIsBits) toFunctionWrappersWrapperSingleCache+AllowNonIsBits— zero-allocation fallback for non-isbits types (BigFloat, SparseConnectivityTracer, etc.) while still catching isbits mismatchesVal{true}/Val{false}andFunctionWrappersWrapper{FW, Bool}APIsDesign
Two orthogonal configuration axes replace the boolean
FBtype parameter:Cache modes (how fallback
FunctionWrappers are cached):NoCacheobj[](arg...)every callSingleCacheFunctionWrapperfor last-seen arg typesDictCacheDictFallback policies (when fallback is allowed):
StrictVal{false})AllowAllVal{true})AllowNonIsBitsThe key mechanism: on first fallback hit, a
FunctionWrapper{Any, ActualArgTypes}is created and cached. Subsequent calls use the cached wrapper's ccall path — zero extra allocations, no type parameter on the function (no unwanted specialization).Benchmark results (from SciML/SciMLBase.jl#1301)
Float64 isbits match path: all configurations identical at ~19.5 ns, 0 allocs. No regression.
Backward compatibility
Legacy APIs are preserved and map to the new system:
FunctionWrappersWrapper(f, args, rets, Val{false}())→Strict+NoCacheFunctionWrappersWrapper(f, args, rets, Val{true}())→AllowAll+NoCacheFunctionWrappersWrapper{FW, false}(fwt)→Strict+NoCacheFunctionWrappersWrapper{FW, true}(fwt)→AllowAll+NoCacheTest plan
🤖 Generated with Claude Code